/*
 * Syntelos 'SX'
 * Copyright (C) 1998, 2007  John Pritchard.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301 USA.
 */
package syntelos.sx.methods.s;

import syntelos.sx.Response;
import syntelos.sx.Request;
import syntelos.sys.Capabilities;

import alto.sec.DN;
import alto.sec.Keys;
import alto.sec.pkcs.PKCS12;
import alto.sec.x509.AVA;
import alto.sec.x509.AVAKeyword;
import alto.sec.x509.X500Name;
import alto.sec.x509.X509Certificate;

import alto.io.Authentication;
import alto.io.Input;
import alto.io.Output;
import alto.io.Principal;
import alto.io.Uri;
import alto.lang.Buffer;
import alto.lang.Component;
import alto.lang.InputStream;
import alto.lang.Type;
import alto.sys.FileManager;
import alto.sys.Reference;

/**
 * Authenticate as 'role' for path to '/s/{UID}' (with or without any
 * fext) to list roles in {UID}.
 * 
 * @see alto.io.Authentication#NotApp
 * @see syntelos.sys.Capabilities$RoleRead
 * @since 1.6
 */
public class Roles
    extends Abstract
{

    public Roles(){
        super();
    }


    public void respond(Request req)
        throws java.io.IOException,
               java.lang.InterruptedException
    {
        super.respond(req);

        String callerId = IdentifierFor(req);
        if (null != callerId){
            Keys caller = Keys.Tools.Dereference(callerId);
            if (null != caller){
                if (this.isAuthenticated(Authentication.NotApp,Capabilities.RoleRead.Instance,caller)){
                    this.setContentTypeText();
                    alto.sec.Role[] list = caller.listRoles();
                    if (null != list){
                        Output out = this.openOutput();
                        try {
                            for (int cc = 0, count = list.length; cc < count; cc++){
                                alto.sec.Role item = list[cc];
                                out.println(item.getUID());
                            }
                        }
                        finally {
                            out.close();
                        }
                    }
                    this.setStatusOk();
                }
                else
                    this.setStatusUnauthorized();
            }
            else
                this.setStatusNotFound();
        }
        else
            this.setStatusBadRequest();
    }
}
